package com.collect.zambia.zhiScene.zhiCollection.zhiAdmin;

import android.content.Context;

import com.collect.zambia.zhiBase.BasePresenter;
import com.collect.zambia.zhiBase.BaseSubscriber;
import com.collect.zambia.zhiBean.zhiReceive.ZHIRGroupPerformanceMoneyEntity;
import com.collect.zambia.zhiBean.zhiReceive.ZHIRGroupPerformanceOrderEntity;
import com.collect.zambia.zhiBean.zhiReceive.ZHIRGroupUserEntity;
import com.collect.zambia.zhiBean.zhiSend.ZHISendEntity;
import com.collect.zambia.zhiBean.zhiSend.ZHISGroupUserPerformanceEntity;
import com.collect.zambia.zhiHelper.ZHIMainProcessHelper;
import com.collect.zambia.zhiNetApi.ZHIHttpApiProvider;
import com.collect.zambia.zhiNetApi.ZHIHttpResponse;
import com.collect.zambia.zhiUtils.RxSchedulers;

import java.util.ArrayList;

import javax.inject.Inject;

public class ZHIAdminPresenter extends BasePresenter<ZHIAdminContract.View> implements ZHIAdminContract.Presenter {

    @Inject
    public ZHIAdminPresenter() {

    }

    @Override
    public void getGroupUserPerformanceMoney(Context context, final ZHISGroupUserPerformanceEntity zhisGroupUserPerformanceEntity, boolean showDialog) {
        BaseSubscriber<ZHIHttpResponse<ArrayList<ZHIRGroupPerformanceMoneyEntity>>> subscriber = new
                BaseSubscriber<ZHIHttpResponse<ArrayList<ZHIRGroupPerformanceMoneyEntity>>>(context, this, showDialog) {
                    @Override
                    protected void onSuccess(Object object) {
                        try {
                            ArrayList<ZHIRGroupPerformanceMoneyEntity> zhirGroupPerformanceMoneyEntityArrayList = (ArrayList<ZHIRGroupPerformanceMoneyEntity>) object;
                            mView.onGetGroupUserPerformanceMoney(zhirGroupPerformanceMoneyEntityArrayList, zhisGroupUserPerformanceEntity);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void onFailure(int code, String messge) {
                        showMessage(messge);
                        mView.onGetErrorInfo();
                    }
                };
        ZHIHttpApiProvider.getApi().myGroupUserPerformanceMoney(zhisGroupUserPerformanceEntity)
                .compose(RxSchedulers.applySchedulers())
                .compose(mView.bindToLife())
                .subscribe(subscriber);
    }

    @Override
    public void getGroupUserPerformanceOrder(Context context, ZHISGroupUserPerformanceEntity zhisGroupUserPerformanceEntity, boolean showDialog) {
        BaseSubscriber<ZHIHttpResponse<ArrayList<ZHIRGroupPerformanceOrderEntity>>> subscriber = new
                BaseSubscriber<ZHIHttpResponse<ArrayList<ZHIRGroupPerformanceOrderEntity>>>(context, this, showDialog) {
                    @Override
                    protected void onSuccess(Object object) {
                        try {
                            ArrayList<ZHIRGroupPerformanceOrderEntity> zhirGroupPerformanceOrderEntityArrayList = (ArrayList<ZHIRGroupPerformanceOrderEntity>) object;
                            if(zhirGroupPerformanceOrderEntityArrayList==null){
                                zhirGroupPerformanceOrderEntityArrayList = new ArrayList<>();
                            }
                            mView.onGetGroupUserPerformanceOrder(zhirGroupPerformanceOrderEntityArrayList, zhisGroupUserPerformanceEntity);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void onFailure(int code, String messge) {
                        showMessage(messge);
                        mView.onGetErrorInfo();
                    }
                };
        ZHIHttpApiProvider.getApi().myGroupUserPerformanceOrder(zhisGroupUserPerformanceEntity)
                .compose(RxSchedulers.applySchedulers())
                .compose(mView.bindToLife())
                .subscribe(subscriber);
    }

    @Override
    public void getGroupUsers(Context context, ZHISendEntity zhiSendEntity) {
        BaseSubscriber<ZHIHttpResponse<ArrayList<ZHIRGroupUserEntity>>> subscriber = new
                BaseSubscriber<ZHIHttpResponse<ArrayList<ZHIRGroupUserEntity>>>(context, this, true) {
                    @Override
                    protected void onSuccess(Object object) {
                        try {
                            ArrayList<ZHIRGroupUserEntity> zhirGroupUserEntityArrayList = (ArrayList<ZHIRGroupUserEntity>) object;
                            if (zhirGroupUserEntityArrayList == null) {
                                zhirGroupUserEntityArrayList = new ArrayList<>();
                            }
                            ZHIAdminReduce.mGroupUserEntityArrayList = zhirGroupUserEntityArrayList;
                            ZHIMainProcessHelper.getInstance().setGroupUserEntityArrayList(zhirGroupUserEntityArrayList);
                            mView.onGetGroupUsers(zhirGroupUserEntityArrayList);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void onFailure(int code, String messge) {
                        showMessage(messge);
                        mView.onGetErrorInfo();
                    }
                };
        ZHIHttpApiProvider.getApi().myGroupUsers(zhiSendEntity)
                .compose(RxSchedulers.applySchedulers())
                .compose(mView.bindToLife())
                .subscribe(subscriber);
    }

}
